package com.have.blog.commons.config.feign;

import com.have.blog.base.constant.BaseSysConstant;
import com.have.blog.base.constant.BlogConstant;
import com.have.blog.base.holder.RequestHolder;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Feign请求拦截器【设置请求头，传递登录信息】
 */
public class FeignBasicAuthRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        HttpServletRequest request = RequestHolder.getRequest();

        String token = null;
        if (request != null) {
            if (request.getHeader("Authorization") != null) {
                token = request.getHeader("Authorization");
            } else if (request.getAttribute(BaseSysConstant.TOKEN) != null) {
                System.out.println("token赋值");
                token = "Bearer " + request.getAttribute(BaseSysConstant.TOKEN).toString();
            }
        }

        requestTemplate.header("token", token);
    }


}
